package thread;

public class Demo7 {

    private static boolean isFinished = false;
    // 此时不再是变量捕获，而是内部类访问外部类成员变量，成员变量的生命周期也是由GC管理的

    public static void main(String[] args) throws InterruptedException {
        //boolean isFinished = false;
        // 不能这样写，因为lambda表达式中的变量需要用final修饰或者即使没有final修饰但并未对该变量进行修改
        Thread thread = new Thread(()->{

            while(!isFinished){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("thread finished");
        });
        thread.start();

        Thread.sleep(1000);
        isFinished = true;
    }
}
